package com.xiegang.serviceMap.service;

import com.google.gson.JsonObject;
import com.xiegang.internalcommon.constant.AmapConfigConstants;
import com.xiegang.internalcommon.constant.CommonStatusEnum;
import com.xiegang.internalcommon.dto.DicDistrict;
import com.xiegang.internalcommon.dto.ResponseResult;
import com.xiegang.serviceMap.mapper.DicDistrictMapper;
import com.xiegang.serviceMap.remote.MapDicDistrictClent;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author xieGang
 * @description
 * @date 2023/3/18 13:25
 **/

@Service
public class DicDistrictService {

    @Autowired
    private MapDicDistrictClent mapDicDistrictClent;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    public ResponseResult initDicDistrict(String keywords) {


        String dicDistrictResult = mapDicDistrictClent.dicDistrict(keywords);
        System.out.println(dicDistrictResult);

        JSONObject dicDistrictJsonObject = JSONObject.fromObject(dicDistrictResult);
        int status = dicDistrictJsonObject.getInt(AmapConfigConstants.STATUS);
        if (status != 1) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getMessage());
        }
        JSONArray countryJsonArray = dicDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);

        for (int country = 0; country < countryJsonArray.size(); country++) {

            JSONObject countyJsonObject = (JSONObject) countryJsonArray.get(country);
            String countyAddressCode = countyJsonObject.getString(AmapConfigConstants.ADCODE);
            String countyAddressName = countyJsonObject.getString(AmapConfigConstants.NAME);
            String countyParentAddressCode = "0";
            String countyLevel = countyJsonObject.getString(AmapConfigConstants.LEVEL);

            insertDicDistrict(countyAddressCode, countyAddressName, countyLevel, countyParentAddressCode);

            JSONArray provinceDistrictJsonArray = countyJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int p = 0; p < provinceDistrictJsonArray.size(); p++) {

                JSONObject provinceDistrictJsonObject = (JSONObject) provinceDistrictJsonArray.get(p);
                String provinceAddressCode = provinceDistrictJsonObject.getString(AmapConfigConstants.ADCODE);
                String provinceAddressName = provinceDistrictJsonObject.getString(AmapConfigConstants.NAME);
                String provinceParentAddressCode = countyAddressCode;
                String provinceLevel = provinceDistrictJsonObject.getString(AmapConfigConstants.LEVEL);

                insertDicDistrict(provinceAddressCode, provinceAddressName, provinceLevel, provinceParentAddressCode);

                JSONArray cityJsonArray = provinceDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int c = 0; c < cityJsonArray.size(); c++) {

                    JSONObject cityDistrictJsonObject = (JSONObject) cityJsonArray.get(c);
                    String cityAddressCode = cityDistrictJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityAddressName = cityDistrictJsonObject.getString(AmapConfigConstants.NAME);
                    String cityParentAddressCode = provinceAddressCode;
                    String cityLevel = cityDistrictJsonObject.getString(AmapConfigConstants.LEVEL);

                    insertDicDistrict(cityAddressCode, cityAddressName, cityLevel, cityParentAddressCode);

                    JSONArray districtJsonArray = cityDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for (int d = 0; d < districtJsonArray.size(); d++) {

                        JSONObject districtJsonObject = (JSONObject) districtJsonArray.get(d);
                        String districtAddressCode = districtJsonObject.getString(AmapConfigConstants.ADCODE);
                        String districtAddressName = districtJsonObject.getString(AmapConfigConstants.NAME);
                        String districtParentAddressCode = cityAddressCode;
                        String districtLevel = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                        if(districtLevel.equals(AmapConfigConstants.STREET)){
                            continue;
                        }
                        insertDicDistrict(districtAddressCode, districtAddressName, districtLevel, districtParentAddressCode);

                    }
                }
            }

        }


        return ResponseResult.success();
    }

    public void insertDicDistrict(String addressCode, String addressName, String level, String parentAddressCode) {

        DicDistrict dicDistrict = new DicDistrict();
        dicDistrict.setAddressCode(addressCode);
        dicDistrict.setAddressName(addressName);
        int levelInt = generateLevel(level);
        dicDistrict.setLevel(levelInt);
        dicDistrict.setParentAddressCode(parentAddressCode);
        dicDistrictMapper.insert(dicDistrict);
    }

    public int generateLevel(String level) {
        int levelInt = 0;
        switch (level) {
            case "country":
                levelInt = 0;
                break;
            case "province":
                levelInt = 1;
                break;
            case "city":
                levelInt = 2;
                break;
            case "district":
                levelInt = 3;
                break;
            default:
                levelInt = 9;
                break;
        }
        return levelInt;
    }
}
